package com.yzt.minemodule.wallet;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yzt.minemodule.activity.H5Activity;
import com.yzt.minemodule.wallet.dialog.AccountBenefitsDialog;
import com.yzt.zhuju.arouter.RouterMain;
import com.yzt.zhuju.arouter.RouterMine;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.dialog.BaseDialog;
import com.yzt.zhuju.dialog.DiamondRechargeDialog;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.mine.wallet.WalletBean;
import com.yzt.zhuju.network.entity.mine.wallet.WalletConfigBean;
import com.yzt.zhuju.utils.AppDialogUtil;
import com.yzt.zhuju.widget.tuiview.DinTextView;
import com.yzt.minemodule.R;
import com.yzt.minemodule.activity.WalletListActivity;
import com.yzt.minemodule.activity.WithDrawActivity;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 我的钱包
 */
@SuppressLint("SetTextI18n")
@Route(path = RouterMine.ACTIVITY_WALLET)
public class WalletActivity extends BaseActivity {

    // 钻石
    private DinTextView tv_candy_num;

    private TextView tv_gift_income;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, WalletActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet_total_page;
    }

    @Override
    public void installViews() {
        mTitleBar.setTitle("我的钱包");
        mTitleBar.setRightContent("");
        mTitleBar.setRightDrawable(R.mipmap.icon_wallet_more);
        mTitleBar.setRightBtnListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseDialog.showDialog(WalletActivity.this, AccountBenefitsDialog.class);
            }
        });

        tv_candy_num = findViewById(R.id.tv_candy_num);
        tv_gift_income = findViewById(R.id.tv_gift_income);

        findViewById(R.id.diamonds_withdraw_tv).setOnClickListener(this);
        findViewById(R.id.tv_withdraw).setOnClickListener(this);
        findViewById(R.id.tv_bank_card).setOnClickListener(this);
        findViewById(R.id.tv_coins_detail).setOnClickListener(this);
        findViewById(R.id.tv_input_detail).setOnClickListener(this);
        findViewById(R.id.tv_history_order).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private WalletBean walletEntry;

    /**
     * 获取钱包信息
     */
    public void initData() {
        BizLayer.getInstance().getMineModule().wallet(new AresSubscriber<WalletBean>() {
            @Override
            protected void onSuccess(WalletBean wallet) {
                walletEntry = wallet;
                tv_candy_num.setText(walletEntry.getCandyNum() + "");
                tv_gift_income.setText(walletEntry.getGiftIncome() + "");
            }
        });
    }

    @Override
    protected void onClick(int clickId) {
        super.onClick(clickId);
        if (clickId == R.id.diamonds_withdraw_tv) {
            // 钻石充值
            Bundle bundle = new Bundle();
            bundle.putBoolean("dark", true);
            DiamondRechargeDialog.showDialog(this, DiamondRechargeDialog.class, bundle);
        }
        if (clickId == R.id.tv_withdraw) {
            if (walletEntry.getWalletOpenStatus() == 1) {
                WithDrawActivity.startSelf(this, walletEntry.getGiftIncome() + "", walletEntry.getBankCardNo(), walletEntry.getRate());
            } else {
                getWalletOpen();
            }
        }

        if (clickId == R.id.tv_bank_card) {
            if (walletEntry.getWalletOpenStatus() == 1) {
                MineBankCardActivity.startSelf(this, walletEntry);
            } else {
                getWalletOpen();
            }
        }

        if (clickId == R.id.tv_coins_detail) {
            WalletListActivity.startSelf(this, 1);
        }

        if (clickId == R.id.tv_input_detail) {
            WalletListActivity.startSelf(this, 2);
        }

        if (clickId == R.id.tv_history_order) {
            WalletListActivity.startSelf(this, 3);
        }
    }

    /**
     * 获取钱包信息
     */
    public void getWalletOpen() {
        BizLayer.getInstance().getMineModule().walletConfig(new AresSubscriber<WalletConfigBean>() {
            @Override
            protected void onSuccess(WalletConfigBean wallet) {
                if (wallet.isAuthed()) {
                    AppDialogUtil.showTitleDialog(mActivity, "提示",
                            "您还未认证个人信息，请先前往个人实名认证",
                            mActivity.getString(R.string.cancel), getString(R.string.sure), new AppDialogUtil.DialogClickInterface() {
                                @Override
                                public void onCancelClick() {

                                }

                                @Override
                                public void onSureClick() {
                                    Intent intent = new Intent(WalletActivity.this, UserAuthActivity.class);
                                    startActivity(intent);
                                }
                            });
                } else {
                      RouterMain.goToWeb(wallet.getOpenUrl());
//                    Intent intent = new Intent(WalletActivity.this, H5Activity.class);
//                    intent.putExtra("url", wallet.getOpenUrl());
//                    intent.putExtra("view_file", false);
//                    startActivity(intent);
                }
            }
        });
    }
}